package com.quasar.hpatient.view.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.hyphenate.chat.EMClient;
import com.quasar.hpatient.MainAppliaction;
import com.quasar.hpatient.R;
import com.quasar.hpatient.alarmUtils.AlarmClockUtils;
import com.quasar.hpatient.base.BaseFragment;
import com.quasar.hpatient.constant.ConstantManger;
import com.quasar.hpatient.model.custommodel.RemindMedicineTableItem;
import com.quasar.hpatient.model.custommodel.TemperatureTableItem;
import com.quasar.hpatient.model.event.UpdateEvent;
import com.quasar.hpatient.model.httpmodel.AnotherResult;
import com.quasar.hpatient.model.httpmodel.DataStateResult;
import com.quasar.hpatient.model.litePalmodel.MedicineInfodb;
import com.quasar.hpatient.model.medicalmodel.CarouselData;
import com.quasar.hpatient.model.medicalmodel.CollectionListData;
import com.quasar.hpatient.model.medicalmodel.ConsultationBean;
import com.quasar.hpatient.model.medicalmodel.MedicationRecordData;
import com.quasar.hpatient.model.medicalmodel.OnedayMedicationData;
import com.quasar.hpatient.model.medicalmodel.PushinfoData;
import com.quasar.hpatient.network.RetrofitConnection;
import com.quasar.hpatient.onInterface.OnConsultationListener;
import com.quasar.hpatient.onInterface.OnJpshListener;
import com.quasar.hpatient.onInterface.OnMedicationListener;
import com.quasar.hpatient.presenter.JpshPresenter;
import com.quasar.hpatient.presenter.RecommendListPresenter;
import com.quasar.hpatient.presenter.RemindMedicinePresenter;
import com.quasar.hpatient.utils.DateUtil;
import com.quasar.hpatient.utils.GlideImageLoader;
import com.quasar.hpatient.utils.MsgJumpView;
import com.quasar.hpatient.utils.TimeZoneUtil;
import com.quasar.hpatient.view.LoginActivity_;
import com.quasar.hpatient.view.adapter.MessageAdapter;
import com.quasar.hpatient.view.adapter.RemindMedicineAdapter;

import com.quasar.hpatient.view.message.MyMessageActivity_;
import com.quasar.hpatient.view.personal.collection.CounsultWebViewActivity_;
import com.quasar.hpatient.view.prescription.AtPrescriptionActivity_;
import com.quasar.hpatient.view.prescription.HistoryLifeActivity_;
import com.quasar.hpatient.view.prescription.MedicationActivity_;
import com.quasar.hpatient.view.record.BloodPressureActivity_;
import com.quasar.hpatient.view.record.BloodSugarActivity_;
import com.quasar.hpatient.view.record.EventActivity_;
import com.quasar.hpatient.view.record.HeartRateActivity_;
import com.quasar.hpatient.view.record.IntakeOutputActivity_;
import com.quasar.hpatient.view.record.MoreRecodeActivity_;
import com.quasar.hpatient.view.record.TemperatureActivity_;
import com.quasar.hpatient.view.record.TestRecordActivity_;
import com.quasar.hpatient.widget.MyMenuView;
import com.rey.material.app.Dialog;
import com.rey.material.app.DialogFragment;
import com.rey.material.app.SimpleDialog;
import com.vise.log.ViseLog;
import com.youth.banner.Banner;
import com.youth.banner.listener.OnBannerListener;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EFragment;
import org.androidannotations.annotations.ViewById;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.List;

/**
 * author : 许建宏
 * Date: 2017/9/29 0029
 * View:首页
 */

@EFragment(R.layout.fragment_home)
public class HomeFragment extends BaseFragment implements OnMedicationListener, OnJpshListener, OnConsultationListener {

    //保存轮播图地址
    List<String> list = new ArrayList<>();
    private RemindMedicineAdapter remindMedicineAdapter;
    private RemindMedicinePresenter remindMedicinePresenter;
    //保存当前服药单信息
    List<RemindMedicineTableItem> remindMedicineTableItem;
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    //时间戳转换
    long startDay;
    //当前时间的时间戳
    long newStatsDay;
    int medicineStats = ConstantManger.MEDICINEBORMAL;//正常
    private JpshPresenter jpshPresenter;
    //分页
    int page = 1;
    private MessageAdapter messageAdapter;
    //咨询列表
    private RecommendListPresenter recommendListPresenter;

    //添加药品
    @ViewById(R.id.ll_add_drug)
    LinearLayout ll_add_drug;
    //图片轮播
    @ViewById(R.id.banner)
    Banner banner;
    @ViewById(R.id.rv_list)
    RecyclerView rv_list;
    //体温
    @ViewById(R.id.mv_temperature)
    MyMenuView mv_temperature;
    //推送的消息
    @ViewById(R.id.rv_jpsh_msg)
    RecyclerView rv_jpsh_msg;

    @ViewById(R.id.main_toolbar)
    Toolbar main_toolbar;

    @AfterViews
    void init() {
        initView();
        initData();
    }

    @Override
    public void initView() {
        main_toolbar.setTitle("首页");

        rv_list.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv_list.setHasFixedSize(true);
        rv_list.setNestedScrollingEnabled(false);

        rv_jpsh_msg.setLayoutManager(new LinearLayoutManager(getActivity()));
        rv_jpsh_msg.setHasFixedSize(true);
        rv_jpsh_msg.setNestedScrollingEnabled(false);
    }


    @Override
    public void initData() {

        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        //咨询轮播
        recommendListPresenter = new RecommendListPresenter(this);
        recommendListPresenter.GetCarouselInformationsData("1");//轮播分页1

        //极光推送的消息
        jpshPresenter = new JpshPresenter(this);
        jpshPresenter.GetUserLatestMessageData(page + "");
        //获取复诊的信息
        jpshPresenter.VisitMessage();

        remindMedicinePresenter = new RemindMedicinePresenter(this);
        //查询当日服药单信息
        remindMedicinePresenter.GetPatientSingleDoseDetailData(TimeZoneUtil.sdf.format(new Date()));
        //推送的消息初始化
        initJpshAdapter();
    }

    //添加药品界面
    @Click(R.id.ll_add_drug)
    public void ll_add_drug() {
        MedicationActivity_.intent(getActivity()).start();
    }

    //初始化adapter布局
    public void initAdapter(List<RemindMedicineTableItem> data) {
        remindMedicineAdapter = new RemindMedicineAdapter(data);
        rv_list.setAdapter(remindMedicineAdapter);
        //itme的点击事件
        remindMedicineAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {
                switch (view.getId()) {
                    case R.id.remind_med_name:
                        ViseLog.d("输出的药品名称" + remindMedicineAdapter.getData().get(position).getName());
                        if (remindMedicineAdapter.getData().get(position).getName() != null) {
                            msg(remindMedicineAdapter.getData().get(position).getName());
                        }
                        break;

                    case R.id.remind_stats://一种服药
                        if (remindMedicineAdapter.getData().get(position) != null) {
                            RemindMedicineTableItem remindMedicineTableItem = remindMedicineAdapter.getData().get(position);
                            if (remindMedicineTableItem != null) {
                                if (remindMedicineTableItem.getName() != null) {
                                    if (remindMedicineTableItem.getStats() == 0) {

                                        //判断服药是否超时，服药时间大于15分钟时状态为1
                                        StringBuffer buffer = new StringBuffer();
                                        buffer.append(sdf.format(new Date()));
                                        buffer.append(" " + remindMedicineTableItem.getMedicineTime());
                                        buffer.append(":00");
                                        ViseLog.d("转换后的时间" + buffer.toString());
                                        //计划时间
                                        startDay = TimeZoneUtil.getStringToDate(buffer.toString(), "yyyy-MM-dd HH:mm:ss");
                                        //实际时间
                                        newStatsDay = TimeZoneUtil.getStringToDate(TimeZoneUtil.sdf.format(new Date()), "yyyy-MM-dd HH:mm:ss");

                                        ViseLog.d("转换后的时间戳:开始时间" + startDay + "结束时间：" + newStatsDay);
                                        //服药超时
                                        if ((newStatsDay - startDay) > 15 * 60 * 1000) {
                                            medicineStats = 2;
                                        }
                                        OneTakeMedicineDiaLog(position, remindMedicineTableItem, medicineStats + "");
                                    }
                                }
                            }
                        }
                        break;

                    case R.id.tv_oneKey://一键服药
                        boolean onclick = false;

                        for (int i = 0; i < remindMedicineAdapter.getData().size(); i++) {
                            if (remindMedicineAdapter.getData().get(i).getName() != null) {
                                if (remindMedicineAdapter.getData().get(i).getStats() == 0) {
                                    onclick = true;
                                }
                            }
                        }
                        if (onclick) {
                            if (remindMedicineAdapter.getData().get(position) != null) {
                                oneMedicineDialog(position);
                            }
                        } else {
                            msg("暂无可服药物");
                        }
                        break;

                    case R.id.textView4://更多
                        MedicationActivity_.intent(getActivity()).start();
                        break;
                }
            }
        });
    }


    //体温
    @Click(R.id.mv_temperature)
    public void mv_temperature() {
        TemperatureActivity_.intent(getActivity()).start();
    }

    //血压
    @Click(R.id.my_blood)
    public void my_blood() {
        BloodPressureActivity_.intent(this).start();
    }

    //血糖
    @Click(R.id.mv_symptom)
    public void mv_symptom() {
        BloodSugarActivity_.intent(getActivity()).start();
    }

    //生命史
    @Click(R.id.mv_healthy)
    public void mv_healthy() {
        HistoryLifeActivity_.intent(this).start();
    }

    //事件
    @Click(R.id.mv_medication)
    public void mv_medication() {
        EventActivity_.intent(getActivity()).start();
    }

    //化验报告
    @Click(R.id.iv_test_record)
    public void iv_test_record() {
        TestRecordActivity_.intent(getActivity()).start();
//        TestDataActivity_.intent(getActivity()).start();

    }

    //服药单
    @Click(R.id.iv_medication)
    public void iv_medication() {
//        MedicationActivity_.intent(getActivity()).start();
        //当前药单
        AtPrescriptionActivity_.intent(getActivity()).start();
    }

    //出入量
    @Click(R.id.mv_motion)
    public void mv_motion() {
        IntakeOutputActivity_.intent(getActivity()).start();
    }

    //其他
    @Click(R.id.mv_other)
    public void mv_other() {
        MoreRecodeActivity_.intent(getActivity()).start();
    }

    //心率
    @Click(R.id.mv_heartRate)
    public void mv_heartRate() {
        HeartRateActivity_.intent(getActivity()).start();
    }

    @Override
    public void onSuccess(AnotherResult<OnedayMedicationData> data) {
        boolean isFirst = true;

        remindMedicineTableItem = new ArrayList<>();
        List<RemindMedicineTableItem> MedicineTableItem = new ArrayList<>();
        if (data != null && data.getList() != null) {
            //判断data.getList().size()大于5条只显示5条，并且第一条必须显示已经服的药品
            int count = data.getList().size();
            for (int i = 0; i < count; i++) {
                RemindMedicineTableItem remindMedi = new RemindMedicineTableItem();
                remindMedi.setItemType(RemindMedicineTableItem.BODY);//布局
                remindMedi.setId(data.getList().get(i).getId());//药品id
                remindMedi.setName(data.getList().get(i).getMName());//药品名称
                remindMedi.setDose(data.getList().get(i).getDoseName());//药品规格
                remindMedi.setSpecifications(data.getList().get(i).getUseCount() + "");//药品剂量
                remindMedi.setMedicineTime(data.getList().get(i).getUseTime().substring(10));//服药时间
                remindMedi.setStats(data.getList().get(i).getStatus());//服药状态
                remindMedi.setDetailId(data.getList().get(i).getDetailId());//药单id

                //如果有服的药物，第一条必须显示已服的药品
                if (isFirst) {
                    if (data.getList().get(i).getStatus() != 0) {
                        remindMedicineTableItem.add(0, remindMedi);
                        isFirst = false;
                    } else {
                        remindMedicineTableItem.add(remindMedi);
                    }
                } else {
                    remindMedicineTableItem.add(remindMedi);
                }
//                //设置闹钟
//
                SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");
                String Time = null;
                try {
                    Time = dateFormat.format(TimeZoneUtil.sdf.parse(data.getList().get(i).getUseTime()));
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                if (data.getList().get(i).getStatus() == 0) {
                    if (data.getList().get(i).getUseTime() != null) {

                        AlarmClockUtils.setClock(getActivity(), Time, i);
                    }
                }
            }
        }

        Date dateTime = new Date();
        String newTime = TimeZoneUtil.sdfFormat.format(dateTime);


        for (int z = 0; z < remindMedicineTableItem.size(); z++) {
            //根据时间戳判断
            StringBuffer stringBuffer = new StringBuffer();
            stringBuffer.append(newTime).append(" " + remindMedicineTableItem.get(z).getMedicineTime());
            try {
                Date oldTime = TimeZoneUtil.sdf.parse(stringBuffer.toString());

                if (dateTime.getTime() > oldTime.getTime()) {
                    continue;
                }
                remindMedicineTableItem.get(z).setMedicineTime(stringBuffer.toString().substring(10, stringBuffer.toString().length() - 3));

            } catch (Exception e) {
                e.printStackTrace();
            }

            MedicineTableItem.add(remindMedicineTableItem.get(z));
        }


        if (data.getList() != null) {
            if (data.getList().size() > 0) {
                ll_add_drug.setVisibility(View.GONE);
                if (remindMedicineTableItem.size() > 0) {
                    MedicineTableItem.add(0, new RemindMedicineTableItem(RemindMedicineTableItem.TAIL));
                    MedicineTableItem.add(1, new RemindMedicineTableItem(RemindMedicineTableItem.HEAD));
                }
            } else {
                ll_add_drug.setVisibility(View.VISIBLE);
            }
        }


        List<RemindMedicineTableItem> data1 = new ArrayList<>();

        int size = MedicineTableItem.size();
        if (size < 7) {
            initAdapter(MedicineTableItem);
        } else {
            for (int i = 0; i < 7; i++) {
                data1.add(MedicineTableItem.get(i));
            }
            initAdapter(data1);
        }

    }

    @Override
    public void onError(String msg) {

        if (msg.equals("0x00000031")) {
            EMClient.getInstance().logout(true); //登陆前先退出，保证登陆成功！
            LoginActivity_.intent(getActivity()).start();
        }
        if (msg != null) {
            msg(msg);
        }
    }

    @Override
    public void onRefreshUI() {
        remindMedicineAdapter.notifyDataSetChanged();
    }


    //一键服药信息提交
    @Override
    public void onAddMutiTakeMedicine(DataStateResult dataStateResult) {

        ViseLog.d("AddOneTakeMedicine" + dataStateResult.getMsgCode());
        if (dataStateResult.getMsgCode().equals("0x00000000")) {
            //服药成功重新请求接口
            //查询当日服药单信息
            ViseLog.d("服药成功" + dataStateResult.getMsgCode());
            remindMedicinePresenter.GetPatientSingleDoseDetailData(TimeZoneUtil.sdf.format(new Date()));
        }

    }

    //一种服药信息提交
    @Override
    public void AddOneTakeMedicine(DataStateResult dataStateResult) {

        ViseLog.d("AddOneTakeMedicine" + dataStateResult.getMsgCode());
        if (dataStateResult.getMsgCode().equals("0x00000000")) {
            //服药成功重新请求接口
            //查询当日服药单信息
            ViseLog.d("服药成功" + dataStateResult.getMsgCode());
            remindMedicinePresenter.GetPatientSingleDoseDetailData(TimeZoneUtil.sdf.format(new Date()));
        }
    }

    @Override
    public void onSuccessMedicationRecordDate(AnotherResult<MedicationRecordData> data) {

    }

    //一种服药信息
    public void OneTakeMedicineDiaLog(final int index, final RemindMedicineTableItem remindMedicineTableItem, final String stats) {

        Dialog.Builder builder = new SimpleDialog.Builder(R.style.SimpleDialogLight) {
            @Override
            public void onPositiveActionClicked(DialogFragment fragment) {
                super.onPositiveActionClicked(fragment);
                OneTakeMedicineInterface(index, stats, remindMedicineTableItem);
            }

            @Override
            public void onNegativeActionClicked(DialogFragment fragment) {
                super.onNegativeActionClicked(fragment);
            }
        };

        builder.title("你确定要服药了吗？")
                .positiveAction("确定")
                .negativeAction("取消");
        ((SimpleDialog.Builder) builder).message("药品名称：" + remindMedicineTableItem.getName()
                + "\n药品规格：" + remindMedicineTableItem.getDose()
                + "\n本次用量：" + remindMedicineTableItem.getSpecifications()//+ "×" + recordMedicationItem.getUnit()
                + "\n提醒时间：" + remindMedicineTableItem.getMedicineTime());
        DialogFragment fragment = DialogFragment.newInstance(builder);

        fragment.show(getFragmentManager(), null);
    }

    //一种服药接口调用
    public void OneTakeMedicineInterface(int position, String stats, RemindMedicineTableItem remindMedicineTableItem) {
        String id = remindMedicineAdapter.getData().get(position).getId() + "";
        remindMedicinePresenter.AddOneTakeMedicineData(stats, id, remindMedicineTableItem.getDetailId() + "", TimeZoneUtil.sdf.format(new Date()));
    }


    //一键服药dialog
    public void oneMedicineDialog(final int index) {
        Dialog.Builder builder = new SimpleDialog.Builder(R.style.SimpleDialogLight) {
            @Override
            public void onPositiveActionClicked(DialogFragment fragment) {
                super.onPositiveActionClicked(fragment);
                oneMedicineInterface(index);
            }

            @Override
            public void onNegativeActionClicked(DialogFragment fragment) {
                super.onNegativeActionClicked(fragment);
            }
        };

        builder.title("确定药物已服用吗？")
                .positiveAction("确定")
                .negativeAction("取消");
        ((SimpleDialog.Builder) builder)
                .message("注：确认后您还可以到当天的用药记录里进行详细修改。");
        DialogFragment fragment = DialogFragment.newInstance(builder);
        fragment.show(getFragmentManager(), null);
    }

    //服药接口调用
    public void oneMedicineInterface(int index) {
        //默认从头部往下第三个开始获取DetailId()
        if (remindMedicineAdapter.getData().get(remindMedicineAdapter.getData().size() - 1).getDetailId() != 0) {
            ViseLog.d("获取药单id+oneMedicineInterface" + remindMedicineAdapter.getData().get(remindMedicineAdapter.getData().size() - 1).getDetailId());
            String id = remindMedicineAdapter.getData().get(remindMedicineAdapter.getData().size() - 1).getDetailId() + "";//药单id
            remindMedicinePresenter.AddMutiTakeMedicineData(id);
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //刷新界面
    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(UpdateEvent updateEvent) {
        if (updateEvent.type.equals(ConstantManger.MYGUESS)) {
            //查询当日服药单信息
            remindMedicinePresenter.GetPatientSingleDoseDetailData(TimeZoneUtil.sdf.format(new Date()));
            ViseLog.d("刷新home药单界面");
        } else if (updateEvent.type.equals(ConstantManger.JPSHMAG)) {
            jpshPresenter.GetUserLatestMessageData(page + "");
            //获取复诊的信息
            jpshPresenter.VisitMessage();
        }
    }

    /**
     * 极光推送的消息
     */
    //初始化adapter布局
    public void initJpshAdapter() {
        messageAdapter = new MessageAdapter();
        rv_jpsh_msg.setAdapter(messageAdapter);

        messageAdapter.setOnItemChildClickListener(new BaseQuickAdapter.OnItemChildClickListener() {
            @Override
            public void onItemChildClick(BaseQuickAdapter adapter, View view, int position) {

                switch (view.getId()) {
                    case R.id.look://查看消息
                        jpshPresenter.UpdateUserMessage(messageAdapter.getData().get(position).getId() + "");
                        MsgJumpView.JumpView(getActivity(), messageAdapter.getData().get(position).getMessageType() + "",
                                testTimeDate(position));
                        break;

                    case R.id.list_itease_layout:
                        ViseLog.d("获取的消息时间为"+messageAdapter.getData().get(position).getTestTime());
                        MsgJumpView.JumpView(getActivity(), messageAdapter.getData().get(position).getMessageType() + "",
                                testTimeDate(position));
                        break;
                }
            }
        });
    }

    //获取消息时间
    public String testTimeDate(int position){
        StringBuffer stringBuffer=new StringBuffer();
        if(messageAdapter.getData().get(position).getTestTime()!=null && messageAdapter.getData().get(position).getTestTime().length()>0){
            stringBuffer.append(messageAdapter.getData().get(position).getTestTime().substring(0, 10));
        }else{
            stringBuffer.append(messageAdapter.getData().get(position).getLogTime().substring(0, 10));
        }
       return stringBuffer.toString();
    }

    @Override
    public void onJpshSuccess(AnotherResult<PushinfoData> data) {
        List<PushinfoData> pushinfoData = new ArrayList<>();
        String nowData = TimeZoneUtil.sdfFormat.format(new Date());
        if (data.getList() != null && data.getList().size() > 0) {
            for (int i = 0; i < data.getList().size(); i++) {
                if (data.getList().get(i).getLogTime().substring(0, 10).equals(nowData) || data.getList().get(i).getStatus()==0) {
                    pushinfoData.add(data.getList().get(i));
                }
            }
        }
        messageAdapter.setNewData(pushinfoData);
    }

    @Override
    public void onJpshError(String msg) {
        if (msg != null) {
            msg(msg);
        }
    }

    //跳转更多消息
    @Click(R.id.tv_more_msg)
    public void tv_more_msg() {
        MyMessageActivity_.intent(this).start();
    }

    @Override
    public void onJpshRefreshUI() {
        messageAdapter.notifyDataSetChanged();
    }

    @Override
    public void onLookSuccessMsg(DataStateResult dataStateResult) {
        if (dataStateResult.getMsgCode().equals("0x00000000")) {
            jpshPresenter.GetUserLatestMessageData(1 + "");
        }
    }

    List<PushinfoData> pushinfoDataList;

    //查看复诊的信息
    @Override
    public void onVisitMessageSuccess(final AnotherResult<PushinfoData> data) {
        pushinfoDataList = new ArrayList<>();
        Resources resources = getActivity().getBaseContext().getResources();
        if (data.getList().size() > 0) {
            pushinfoDataList.addAll(data.getList());
            Drawable drawable1 = resources.getDrawable(R.drawable.btn_rad_green);
            tv_msg_review.setBackground(drawable1);
        } else {
            Drawable drawable2 = resources.getDrawable(R.color.btpresscolor);
            tv_msg_review.setBackground(drawable2);
        }

        for (int i = 0; i < data.getList().size(); i++) {
            String time = data.getList().get(i).getLogTime().substring(0, 10);
            ViseLog.d("复诊的时间" + TimeZoneUtil.preMsg(time, -1) + "----" + TimeZoneUtil.sdfFormat.format(new Date()));
            ViseLog.d("复诊的时间" + TimeZoneUtil.preMsg(time, -2) + "----" + TimeZoneUtil.sdfFormat.format(new Date()));
            ViseLog.d("复诊的时间" + TimeZoneUtil.preMsg(time, -3) + "----" + TimeZoneUtil.sdfFormat.format(new Date()));
            if (
                    TimeZoneUtil.preMsg(time, -1).equals(TimeZoneUtil.sdfFormat.format(new Date()))
                            || TimeZoneUtil.preMsg(time, -2).equals(TimeZoneUtil.sdfFormat.format(new Date()))
                            || TimeZoneUtil.preMsg(time, -3).equals(TimeZoneUtil.sdfFormat.format(new Date()))
                            || time.equals(TimeZoneUtil.sdfFormat.format(new Date()))) {

                AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
                final int finalI = i;
                dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                        dialogVisit(finalI, data.getList());

                    }
                }).setMessage(data.getList().get(i).getMessage()).show();
                break;
            }
        }
    }

    //复诊dialog
    public void dialogVisit(final int index, final List<PushinfoData> pushinfoData) {
        Dialog.Builder builder = new SimpleDialog.Builder(R.style.SimpleDialogLight) {
            @Override
            public void onPositiveActionClicked(DialogFragment fragment) {
                super.onPositiveActionClicked(fragment);

            }

            @Override
            public void onNegativeActionClicked(DialogFragment fragment) {
                super.onNegativeActionClicked(fragment);
                jpshPresenter.UpdateUserMessage(pushinfoData.get(index).getId() + "");
            }
        };
        builder.title("已记住下次不提示？")
                .positiveAction("提示")
                .negativeAction("不提示");
        ((SimpleDialog.Builder) builder).message("");
        DialogFragment fragment = DialogFragment.newInstance(builder);

        fragment.show(getFragmentManager(), null);
    }


    /**
     * 咨询轮播列表
     */

    @Override
    public void onCarouselSuccess(final AnotherResult<CarouselData> dataAnotherResult) {
        list.clear();
        if (dataAnotherResult != null && dataAnotherResult.getList() != null) {
            if (dataAnotherResult.getList().size() > 0) {
                for (int i = 0; i < dataAnotherResult.getList().size(); i++) {
                    list.add(RetrofitConnection.PHOTOURL + dataAnotherResult.getList().get(i).getCover());
                }

                ViseLog.d("点击了咨询轮播");
                banner.setImages(list)
                        .setImageLoader(new GlideImageLoader())
                        .setOnBannerListener(new OnBannerListener() {
                            @Override
                            public void OnBannerClick(int position) {

                                ViseLog.d("点击了咨询轮播");

                                if (list.size() != 0) {
                                    CounsultWebViewActivity_.intent(getActivity()).recordId(dataAnotherResult.getList().get(position).getId() + "").start();
                                }
                            }
                        })
                        .start();
            }
        }
    }

    @Override
    public void onMyConsultationListSuccess(AnotherResult<CollectionListData> dataAnotherResult) {

    }

    @Override
    public void onNottoCollectSuccess(DataStateResult dataStateResult) {

    }

    @Override
    public void onButtonCollectionSuccess(DataStateResult dataStateResult) {

    }

    @Override
    public void onConsultationListSuccess(AnotherResult<ConsultationBean> data) {
    }

    @Override
    public void onConsultationRefreshUI() {

    }

    @Override
    public void OnSeccessLast(AnotherResult<OnedayMedicationData> data) {

    }

    @Override
    public void OnSeccessNext(AnotherResult<OnedayMedicationData> data) {

    }

    @ViewById(R.id.tv_msg_review)
    TextView tv_msg_review;

    //查看复查消息
    @Click(R.id.tv_msg_review)
    public void tv_msg_review() {


        if (pushinfoDataList != null && pushinfoDataList.size() > 0) {

            Collections.sort(pushinfoDataList, new Comparator<PushinfoData>() {
                @Override
                public int compare(PushinfoData o1, PushinfoData o2) {
                    Date date1 = DateUtil.stringToDate(o1.getLogTime());
                    Date date2 = DateUtil.stringToDate(o2.getLogTime());
                    if (date1.after(date2)) {
                        return 1;
                    }
                    return -1;
                }
            });

            reviewDialog(pushinfoDataList);
        }
    }

    //弹出复诊的信息
    public void reviewDialog(List<PushinfoData> pushinfoDataList) {
        StringBuffer stringBuffer = new StringBuffer();
        for (int i = 0; i < pushinfoDataList.size(); i++) {
            PushinfoData pushinfoData = pushinfoDataList.get(i);
            stringBuffer.append(pushinfoData.getMessage() + "\n\n");
        }
        AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
        dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
            }
        }).setMessage(stringBuffer.toString()).show();
    }
}
